<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>文章分类</span>
      <el-button style="float: right" type="primary" @click="addible = true">添加分类</el-button>
    </div>
    <div class="text item">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="序号" width="180px"> </el-table-column>
        <el-table-column prop="cate_name" label="分类"> </el-table-column>
        <el-table-column prop="cate_alias" label="别名"> </el-table-column>
        <el-table-column prop="name" label="操作">
          <template slot-scope="scope">
            <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
            <el-button type="success" @click="updata(scope.row.id)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加分类的弹出框-->
    <el-dialog title="添加分类" :visible.sync="addible" width="450px" :before-close="cancal">
      <el-form ref="form" :rules="caterules" :model="cateData">
        <el-form-item label="分类名称" label-width="80px" prop="cate_name">
          <el-input v-model="cateData.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" label-width="80px" prop="cate_alias">
          <el-input v-model="cateData.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancal">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改分类的弹出框-->
    <el-dialog title="修改分类" :visible.sync="updataible" width="450px" :before-close="cancal">
      <el-form ref="form" :rules="caterules" :model="updataCates">
        <el-form-item label="分类名称" label-width="80px" prop="cate_name">
          <el-input v-model="updataCates.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" label-width="80px" prop="cate_alias">
          <el-input v-model="updataCates.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancal">取 消</el-button>
        <el-button type="primary" @click="updataCate">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { addCateAPI, delCateAPI, getUpdataCateAPI, UpdataCateAPI } from '@/api/cate'
export default {
  name: 'ArtCate',
  data() {
    return {
      // 表格数据
      tableData: [...this.$store.state.cate.cateList],
      // 控制添加卡片显示隐藏
      addible: false,
      // 控制修改卡片显示隐藏
      updataible: false,
      // 添加卡片数据
      cateData: {
        cate_name: '',
        cate_alias: ''
      },
      // 修改卡片数据
      updataCates: {
        id: '',
        cate_name: '',
        cate_alias: ''
      },
      // 验证信息
      caterules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { min: 1, max: 10, message: '长度1-10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '只能是长度1~15位的字母数字组合',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.$store.dispatch('cate/useGet')
  },
  // 侦听vuex数据变化，给tableData赋值
  watch: {
    '$store.state.cate.cateList': {
      handler(val) {
        this.tableData = val
      },
      deep: true
    }
  },
  methods: {
    // 删除
    del(id) {
      this.$confirm('确定要删除嘛？？?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await delCateAPI(id)
          if (res.code === 0) {
            this.$message.success(res.message)
            this.$store.dispatch('cate/useGet')
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(() => {
          this.$message.error('已取消删除')
        })
    },
    // 获取修改信息
    async updata(id) {
      this.updataCates.id = id
      this.updataible = true
      const { data: res } = await getUpdataCateAPI(id)
      if (res.code === 0) {
        this.updataCates = res.data
      } else {
        this.$message.error(res.message)
      }
    },
    // 确认修改
    updataCate() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data: res } = await UpdataCateAPI(this.updataCates)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.updataible = false
          this.$refs.form.resetFields()
          this.$store.dispatch('cate/useGet')
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 添加
    addCate() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCateAPI(this.cateData)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.$refs.form.resetFields()
          this.addible = false
          this.$store.dispatch('cate/useGet')
          // this.tableData = [...this.$store.state.cate.cateList]
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 取消
    cancal() {
      this.$refs.form.resetFields()
      this.addible = false
      this.updataible = false
    }
  }
}
</script>

<style lang="" scoped></style>
